﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Service;
using S4Store.Models.SellerKpi;
using S4Store.Domain.SellerKpi;
using S4Store.Domain;

namespace S4Store.Controllers
{
    public class SellerKpiController : ReportBaseController
    {
        public ReportService ReportService { get; set; }
        public SellerKpiService SellerKpiService { get; set; }
        public EmployeeService EmployeeService { get; set; }
        public CarService CarService { get; set; }
        public CarKpiService CarKpiService { get; set; }
        
        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            InjectReportModel(model);
            using (var t = new Transtion())
            {
                t.Inject(SellerKpiService);
                var data = SellerKpiService.GetSellerKpiDayDataQuery().Where(x => x.Seller.StoreId == Store.Id).ToList();
                model.MonthDayData = data;
                
                return View(model);
            }
        }

        public ActionResult MonthTd(int y,int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(SellerKpiService);
                var config = SellerKpiService.GetSellerKpiMonthConfig(y, m, Store.Id);
                if (config == null)
                {
                    config = new Domain.SellerKpi.SellerKpiMonthConfig();
                    config.StoreId = Store.Id;
                    config.Month = new DateTime(y, m, 1);
                }
                return View(config);
            }
            
        }

        public ActionResult InputSellerKpiMonthConfig(int y,int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(SellerKpiService);
                var config = SellerKpiService.GetSellerKpiMonthConfig(y, m, Store.Id);
                if (config == null )
                {
                    config = new Domain.SellerKpi.SellerKpiMonthConfig();
                    config.StoreId = Store.Id;
                    config.Month = new DateTime(y, m, 1);
                }
                return View(config);
            }
        }

        public JsonResult ModifyBackInputSellerKpiMonthConfig(S4Store.Domain.SellerKpi.SellerKpiMonthConfig config, List<SellerMonthConfig> seller, bool isconfirm=false)
        {
            using (var t = new Transtion())
            {
                t.Inject(SellerKpiService);
                var dbconfig = SellerKpiService.GetSellerKpiMonthConfigQuery().FirstOrDefault(x => x.Id == config.Id);
                if (dbconfig == null)
                {
                    dbconfig = new Domain.SellerKpi.SellerKpiMonthConfig();
                }
                if (isconfirm)
                {
                    dbconfig.IsConfirmed = true;
                }
                dbconfig.StoreId = Store.Id;
                dbconfig.Month = config.Month;
                dbconfig.LastMonthAvgvr = config.LastMonthAvgvr;                
                SellerKpiService.SubmitSellerKpiMonthConfig(dbconfig);

                if (seller == null)
                {
                    seller = new List<SellerMonthConfig>();
                }
                foreach (var sel in seller)
                {
                    sel.Month = config.Month;
                    if (isconfirm)
                    {
                        sel.IsConfirmed = true;
                    }
                }
                SellerKpiService.SubmitSellerMonthConfig(config.Month.Year, config.Month.Month, Store.Id, seller);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }   
        }

        public ActionResult SellerList(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(EmployeeService);
                t.Inject(CarService);
                t.Inject(SellerKpiService);
                t.Inject(CarKpiService);
                SellerListModel model = new SellerListModel();
                var seller = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.FieldSaler);
                model.Sellers = seller.ToList();
                model.CarBrands = CarKpiService.GetCarbrandsForStoreMonth(y, m, Store.Id).ToList();
                model.SellerMonthConfigs = SellerKpiService.GetSellerMonthConfig(y, m, Store.Id, EmployeeType.FieldSaler | EmployeeType.TelSaler).ToList();
                return View(model);
            }
        }

        public ActionResult InputSellerKpiDayData(int y, int m,int d)
        {
            InputSellerKpiDayDataModel model = new InputSellerKpiDayDataModel();
            using (var t = new Transtion())
            {
                t.Inject(EmployeeService);
                t.Inject(SellerKpiService);
                t.Inject(CarService);
                t.Inject(CarKpiService);
                var seller = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.FieldSaler).ToList();
                var brands = CarKpiService.GetCarbrandsForStoreMonth(y, m, Store.Id).ToList();
                model.Sellers = seller;
                model.CarBrands = brands;
                model.Date = new DateTime(y, m, d);
                model.DayDatas = SellerKpiService.GetSellerKpiDayDatas(y, m, d, Store.Id).ToList();
                model.IsConfirmed = false;
                if(model.DayDatas.Any(x=>x.IsConfirmed))
                {
                    model.IsConfirmed = true;
                }
                return View(model);
            }
        }

        public ActionResult ModifyBackInputSellerKpiDayData(List<SellerKpiDayData> rows,int y, int m,int d, bool isconfirm=false)
        {
            foreach (var row in rows)
            {
                if (isconfirm)
                {
                    row.IsConfirmed = true;
                }
                else
                {
                    row.IsConfirmed = false;
                }
            }
            using (var t = new Transtion())
            {
                t.Inject(SellerKpiService);
                SellerKpiService.SubmitSellerKpiDayDatas(y, m, d, Store.Id, rows);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }
    }
}
